package dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import dto.BiletDTO;
import dto.EmployeeDTO;
import entities.Bilet;

@Stateless
public class BiletDAO implements BiletDAORemote {

	@PersistenceContext
	EntityManager em;

	public BiletDAO() {

	}

	@Override
	public BiletDTO findBiletByStatus(BiletDTO biletDTO) {
		Query query = em.createNamedQuery("findBiletByStatus");
		query.setParameter("statusParam", biletDTO.getStatus());
		List<Bilet> bilet = query.getResultList();
		BiletDTO result = new BiletDTO();
		if (bilet != null && bilet.size() > 0) {
			for (Bilet b : bilet) {
				try {
					// undeva stochez, creez un tabel sau nush
					System.out.println("Motiv: " + b.getMotiv());

				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		} else
			System.out.println("Error!!!!");
		return result;
	}

	@Override
	public BiletDTO findBiletByEmployee(BiletDTO biletDTO) {
		Query query = em.createNamedQuery("findBiletByEmployee");
		query.setParameter("usernameParam", biletDTO.getEmpl_id());
		List<Bilet> bilet = query.getResultList();
		BiletDTO result = new BiletDTO();
		if (bilet != null && bilet.size() > 0) {
			for (Bilet b : bilet) {
				try {
					// undeva stochez, creez un tabel sau nush
					System.out.println("Motiv: " + b.getMotiv());

				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		} else
			System.out.println("Error!!!!");
		return result;
	}
	/*
	@Override
	public BiletDTO findBiletByPeriod(BiletDTO biletDTO) {
		Query query = em.createNamedQuery("findBiletByPeriod");
		query.setParameter("periodParam", biletDTO.getStart_time());
		List<Bilet> bilet = query.getResultList();
		BiletDTO result = new BiletDTO();
		if (bilet != null && bilet.size() > 0) {
			for (Bilet b : bilet) {
				try {
					// undeva stochez, creez un tabel sau nush
					System.out.println("Motiv: " + b.getMotiv());

				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		} else
			System.out.println("Error!!!!");
		return result;
	}
	*/
	@Override
	public void addBilet(BiletDTO biletDTO) {
		try {
			Bilet b = new Bilet();
			b.setEmpl_id(biletDTO.getEmpl_id());
			b.setId(biletDTO.getId());
			b.setMotiv(biletDTO.getMotiv());
			b.setStart_time(biletDTO.getStart_time());
			b.setEnd_time(biletDTO.getEnd_time());
			b.setStatus(biletDTO.getStatus());
			b.setTl_id(biletDTO.getTl_id());
			b.setFirstname(biletDTO.getFirstname());
			b.setLastname(biletDTO.getLastname());
			em.persist(b);
		} catch (IllegalArgumentException e) {
			throw new IllegalArgumentException("Illegal argument exception: " + e);
		}
	}

	@Override
	public int findTeamLeaderByAngajat(EmployeeDTO eDTO) {
		return 0;
	}

	@Override
	public List<BiletDTO> findAllTickets(EmployeeDTO eDTO) {
		Query query = em.createNamedQuery("findAllTickets");
		query.setParameter("tl_idParam", eDTO.getId());
		query.setParameter("statusParam", "In asteptare");
		List<Bilet> bilete = query.getResultList();
		List<BiletDTO> bil = new ArrayList<BiletDTO>();
		for (int i = 0; i < bilete.size(); i++) {
			BiletDTO b = new BiletDTO();
			b.setEmpl_id(bilete.get(i).getEmpl_id());
			b.setEnd_time(bilete.get(i).getEnd_time());
			b.setMotiv(bilete.get(i).getMotiv());
			b.setStart_time(bilete.get(i).getStart_time());
			b.setStatus(bilete.get(i).getStatus());
			b.setTl_id(bilete.get(i).getTl_id());
			b.setFirstname(bilete.get(i).getFirstname());
			b.setLastname(bilete.get(i).getLastname());
			b.setId(bilete.get(i).getId());
			bil.add(b);
		}

		return bil;
	}

	@Override
	public BiletDTO aprovaBiletById(int biletId) {
		Bilet b = new Bilet();
		b = em.find(Bilet.class, biletId);
		b.setStatus("Aprobat");
		return null;
	}

	@Override
	public BiletDTO refuzaBiletById(int biletId) {
		Bilet b = new Bilet();
		b = em.find(Bilet.class, biletId);
		b.setStatus("Refuzat");
		return null;
	}

	@Override
	public List<BiletDTO> findAllTickets2(EmployeeDTO eDTO) {
		Query query = em.createNamedQuery("findAllTickets2");
		query.setParameter("empl_idParam", eDTO.getId());
		List<Bilet> bilete = query.getResultList();
		List<BiletDTO> bil = new ArrayList<BiletDTO>();
		for (int i = 0; i < bilete.size(); i++) {
			BiletDTO b = new BiletDTO();
			b.setEmpl_id(bilete.get(i).getEmpl_id());
			b.setEnd_time(bilete.get(i).getEnd_time());
			b.setMotiv(bilete.get(i).getMotiv());
			b.setStart_time(bilete.get(i).getStart_time());
			b.setStatus(bilete.get(i).getStatus());
			b.setTl_id(bilete.get(i).getTl_id());
			b.setFirstname(bilete.get(i).getFirstname());
			b.setLastname(bilete.get(i).getLastname());
			b.setId(bilete.get(i).getId());
			bil.add(b);
		}

		return bil;
	}

	@Override
	public List<BiletDTO> getBileteDupaStatus(String status, int id) {
		Query query = em.createNamedQuery("findBiletByStatusAndEmployee");
		query.setParameter("status_Param", status);
		query.setParameter("idParam", id);
		List<Bilet> bilete = query.getResultList();
		List<BiletDTO> bil = new ArrayList<BiletDTO>();
		for (int i = 0; i < bilete.size(); i++) {
			BiletDTO b = new BiletDTO();
			b.setEmpl_id(bilete.get(i).getEmpl_id());
			b.setEnd_time(bilete.get(i).getEnd_time());
			b.setMotiv(bilete.get(i).getMotiv());
			b.setStart_time(bilete.get(i).getStart_time());
			b.setStatus(bilete.get(i).getStatus());
			b.setTl_id(bilete.get(i).getTl_id());
			b.setFirstname(bilete.get(i).getFirstname());
			b.setLastname(bilete.get(i).getLastname());
			b.setId(bilete.get(i).getId());
			bil.add(b);
		}

		return bil;
	}
	
	public boolean isNumber(String s){
		try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    return true;
	}
	
	public boolean isDateFormat(String s){
		if (s.length() != 10){
			System.out.println("1");
			return false;
		}
		if ('-' != s.charAt(4)){
			System.out.println("2");
			return false;
		}
		if ('-' != s.charAt(7)){
			System.out.println("3");
			return false;
		}
		String an = "";
		an = an + s.charAt(0) + s.charAt(1) + s.charAt(2) + s.charAt(3);
		String lu = "";
		lu = lu + s.charAt(5) + s.charAt(6);
		String zi = "";
		zi = zi + s.charAt(8) + s.charAt(9);
		if (!isNumber(an)){
			System.out.println("4");
			return false;
		}
		if (!isNumber(lu)){
			System.out.println("5");
			return false;
		}
		if (!isNumber(zi)){
			System.out.println("6");
			return false;
		}
		System.out.println("7");
		return true;
	}
	
	public boolean laterThan(String a, String b){
		String an1 = "";
		an1 = an1 + a.charAt(0) + a.charAt(1) + a.charAt(2) + a.charAt(3);
		String lu1 = "";
		lu1 = lu1 + a.charAt(5) + a.charAt(6);
		String zi1 = "";
		zi1 = zi1 + a.charAt(8) + a.charAt(9);
		
		String an2 = "";
		an2 = an2 + b.charAt(0) + b.charAt(1) + b.charAt(2) + b.charAt(3);
		String lu2 = "";
		lu2 = lu2 + b.charAt(5) + b.charAt(6);
		String zi2 = "";
		zi2 = zi2 + b.charAt(8) + b.charAt(9);
		
		int anul1 = Integer.parseInt(an1);
		int anul2 = Integer.parseInt(an2);
		int luna1 = Integer.parseInt(lu1);
		int luna2 = Integer.parseInt(lu2);
		int ziua1 = Integer.parseInt(zi1);
		int ziua2 = Integer.parseInt(zi2);
		
		if (anul1 > anul2)
			return true;
		else if (anul1 == anul2){
			if (luna1 > luna2)
				return true;
			else if (luna1 == luna2)
				if (ziua1 >= ziua2)
					return true;
		}
		return false;
	}

	@Override
	public List<BiletDTO> getBileteDupaData(String valoare, int id) {
		if (isDateFormat(valoare)) {
			Query query = em.createNamedQuery("findBiletByStatus2");
			query.setParameter("idParam", id);
			List<Bilet> bilete = query.getResultList();
			List<BiletDTO> bil = new ArrayList<BiletDTO>();
			for (int i = 0; i < bilete.size(); i++) {
				BiletDTO b = new BiletDTO();
				b.setEmpl_id(bilete.get(i).getEmpl_id());
				b.setEnd_time(bilete.get(i).getEnd_time());
				b.setMotiv(bilete.get(i).getMotiv());
				b.setStart_time(bilete.get(i).getStart_time());
				b.setStatus(bilete.get(i).getStatus());
				b.setTl_id(bilete.get(i).getTl_id());
				b.setFirstname(bilete.get(i).getFirstname());
				b.setLastname(bilete.get(i).getLastname());
				b.setId(bilete.get(i).getId());
				if (laterThan(b.getStart_time(), valoare))
					bil.add(b);
			}

			return bil;

		}
		return null;
	}
}